<?php


namespace app\admin\command;

use app\api\model\WxMessage;
use app\common\model\Setting;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class MonthNotice extends Command
{

    protected function configure()
    {
        $this->setName('month_notice')->setDescription('月卡推送定时器');
    }

    protected function execute(Input $input, Output $output)
    {

        $config = Setting::getItem('sms',10004);
        if(empty($config['month_notice'])){
            return;
        }
        $BeginDate=date('Y-m-01', strtotime(date("Y-m-d",time())));

        $times=date('Y-m-d',time());


        if($times == $BeginDate){

            $model = Db::name('order_goods')
                ->alias('goods')
                ->join('order','goods.order_id = order.order_id','left')
                ->where(['goods.goods_type'=>2,'order.order_status'=>['<',30],'order.pay_status'=>20])
                ->select();


            $user_model = Db::name('user');
            if(!empty($model)){
                foreach ($model as $value) {
                    $open_id = $user_model->where('user_id',$value['user_id'])->value('open_id');
                    if(!$open_id){
                        log_print('用户缺少open_id');
                        continue;
                    }
                    $data = [
                        'openid'=>$open_id,
                        'name'=>$value['goods_name'],
                        'mobile'=>15819406810,
                        'order_str'=>$value['order_no'],
                        'order_status'=>'等待使用',
                        'sum_money'=>$value['total_price']
                    ];
                    var_dump($data);
                    WxMessage::sendMessage(11,$data);
                }
            }
        }

        $output->writeln("通知完成");
    }

}